﻿// $Id: Validation.cs 10 2008-02-29 15:58:02Z mcartoixa $

/* ***************************************************************************
 *                                                                           *
 * Copyright © 2007 NourY Solutions                                          *
 *                                                                           *
 * This file is part of DataRules.                                           *
 *                                                                           *
 * DataRules is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published     *
 * by the Free Software Foundation; either version 3 of the License, or      *
 * (at your option) any later version.                                       *
 *                                                                           *
 * DataRules is distributed in the hope that it will be useful, but          *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this program.  If not, see                             *
 * <http://www.gnu.org/licenses/lgpl.html>.                                  *
 *                                                                           *
 *************************************************************************** */

/*! \file
 * \brief Definition of \c Salamanca.DataRules.NHibernate.Validation.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 fÃ©vr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate.Validator;

namespace Salamanca.DataRules.NHibernate
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>This class contains useful methods to integrate the NHibernate Validator framework
    /// into the DataRules library.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public static class Validation
    {

        /// <summary>Creates the rules for the specified type.</summary>
        /// <param name="targetType">Type for which the rules are created.</param>
        /// <returns>The list of rules.</returns>
        public static IList<IRule> CreateRules(Type targetType)
        {
            List<IRule> ret=new List<IRule>();

            ret.Add(new ValidationRule());

            return ret;
        }

        /// <summary>Registers the Enterprise Library validation provider.</summary>
        /// <remarks>
        ///   <note>This method must be called prior to any validation.</note>
        ///   <para>When this method is called, an event handler is added to the <see cref="Rules.CreatingRules" /> event.
        /// When triggered, it creates the rules specific to this provider.</para>
        /// </remarks>
        public static void RegisterProvider()
        {
            Rules.CreatingRules+=_CreatingRules;
        }

        /// <summary>Unregisters the Enterprise Library validation provider.</summary>
        public static void UnregisterProvider()
        {
            Rules.CreatingRules-=_CreatingRules;
        }

        /// <summary>Creatings the rules.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Salamanca.DataRules.CreatingRulesEventArgs"/> instance containing the event data.</param>
        private static void CreatingRules(object sender, CreatingRulesEventArgs e)
        {
            foreach (IRule r in Validation.CreateRules(e.TargetType))
                e.Rules.Add(r);
        }

        private static EventHandler<CreatingRulesEventArgs> _CreatingRules=new EventHandler<CreatingRulesEventArgs>(CreatingRules);
    }
}
